// tag标签的数据和方法
import { defineStore } from "pinia";
import { ClassTag } from "../../../types/index";
import type { RouteLocationNormalizedLoaded } from 'vue-router'
import router from "@/router"
export const userTagStore = defineStore({
    id: 'TagsStore',
    // persist: {//数据持久化
    //     paths: ['tagsInfo'], // 自定义key
    //     storage: sessionStorage  // 自定义存储方式
    // },
    state: () => ({
        tagList: [new ClassTag("/index/homepage", "home", "home")],
        item: new ClassTag("/index/homepage", "home", "home")
    }),
    actions: {
        /** 
         *   添加标签
         *  @param route 路由对象
         *  @param index 要添加的位置，默认为末尾-1
         *  @return
         */
        addTag(route: RouteLocationNormalizedLoaded, index: number = -1) {
            const { name, meta, fullPath } = route
            this.item = new ClassTag(fullPath, meta.title as string, name as string)
            if ("flush" == name) {//过滤刷新
                return
            }
            let openTag = this.tagList
            let findtag = openTag.find((p: ClassTag) => p.fullPath == fullPath)
            if (!findtag) {
                let i = index > -1 ? index : openTag.length
                openTag.splice(i, 0, new ClassTag(fullPath, meta.title as string, name as string))
            }
        },
        closeTag(index: number) {
            let openTag = this.tagList
            if (this.item.fullPath == openTag[index].fullPath) {
                let item = openTag[index + 1] || openTag[index - 1]
                router.push(item.fullPath)
            }
            openTag.splice(index, 1)
        },
        renovate(index: number) {
            router.replace({ name: 'flush', params: { path: this.tagList[index].fullPath } })
        },
        changeTag(item: any) {
            this.item = item
            router.push(item.fullPath)

        },
        closeAllRightTab(index: number) {
            let i = this.tagList.findIndex(item => item.fullPath == this.item.fullPath)//获取当前选中的下标
            if (i > index) {//当前下标大于操作的下标时                
                router.push(this.tagList[index].fullPath)
            }
            this.tagList.splice(index + 1)
        },
        nameToClose(name: string) {
            let index = this.tagList.findIndex(item => name == item.name)
            this.closeTag(index)
        }

    }
})